package jgl.vajra.kingkong.widget.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.databinding.ViewMultiPicChooseBinding;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.widget.base.BaseCustomView;
import jgl.vajra.kingkong.widget.bean.MultiPicChooseBean;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.interfaces.ImagePickerListener;
import jgl.vajra.kingkong.widget.utils.PickFileToolsV2;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;


/**
 * Created by Administrator on 2017/4/11 0011.
 */

public class MultiPicChooseView extends BaseCustomView<ViewMultiPicChooseBinding> implements BaseBindingItemPresenter<MultiPicChooseBean>  {
    public static final int IMAGE_PICKER = 11;
    private List<MultiPicChooseBean> multiPicChooseBeanList;
    private SingleTypeBindingAdapter adapter;


    private ArrayList<String> pathList = new ArrayList<>();
    private PickFileToolsV2 pickFileTools;
    private int selectImageSize;
    private int selectImageType;
    private int showImageListType;
    private int spanCount;
    //    private Transferee transferee;
//    private TransferConfig config;

    public MultiPicChooseView(Context context) {
        super(context);
    }

    public MultiPicChooseView(Context context, AttributeSet attrs) {
        super(context, attrs);
        multiPicChooseBeanList = new ArrayList<>();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.view_multi_pic_choose;
    }

    @Override
    protected void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MultiPicChooseView);

        spanCount = typedArray.getInteger(R.styleable.MultiPicChooseView_spanCount, 3);
        selectImageSize = typedArray.getInteger(R.styleable.MultiPicChooseView_selectImageSize, 3);
        selectImageType = typedArray.getInteger(R.styleable.MultiPicChooseView_selectType, 1);
        multiPicChooseBeanList = MultiPicChooseBean.getAddImageBean(0, selectImageType);
        if (selectImageType == 2) {
            adapter = new SingleTypeBindingAdapter(context, multiPicChooseBeanList, R.layout.item_multi_pic_choose2);
        } else {
            adapter = new SingleTypeBindingAdapter(context, multiPicChooseBeanList, R.layout.item_multi_pic_choose);
        }
        adapter.setItemPresenter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, spanCount, LinearLayoutManager.VERTICAL, false);
        mBinding.setLayoutManager(gridLayoutManager);
        mBinding.setAdapter(adapter);
        pickFileTools = PickFileToolsV2.init((FragmentActivity) context);
        setNestedScrollingEnabled(false);
        typedArray.recycle();

    }

    @Override
    public void initEvent(Context context) {

    }

    @Override
    public void initView(Context context, View rootView, AttributeSet attrs) {

    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void onItemClick(int position, MultiPicChooseBean itemData) {
        if (showImageListType == 1) return;
        switch (itemData.type) {

            case 0:
                pickFileTools.pick(selectImageSize  ,   new ImagePickerListener() {
                    @Override
                    public void onPickImageList(List<String> imgs, int tag) {
                        if (imgs == null || imgs.size() == 0) {
                            return;
                        }
                        multiPicChooseBeanList.clear();
                        pathList.clear();
                        for (String uri : imgs) {
                            if (pathList.size() > selectImageSize) {
                                break;
                            }
                            pathList.add(uri);

                        }
                        for (String path : pathList) {
                            MultiPicChooseBean pojo = new MultiPicChooseBean();
                            pojo.imgUrlString = path;
                            pojo.type = 1;
                            multiPicChooseBeanList.add(pojo);
                        }
                        Logger.d("multiPicChooseBeanList"+multiPicChooseBeanList.toString());
                        multiPicChooseBeanList.addAll(MultiPicChooseBean.getAddImageBean(pathList.size(), selectImageType));
                        MultiPicChooseBean pojo = getBean();
                        if (pathList.size() >= selectImageSize) {
                            pojo.setVisiable(false);
                        } else {
                            pojo.setVisiable(true);
                        }
                        adapter.refresh(multiPicChooseBeanList);
                    }
                });
                break;
        }
    }

    public void onShowImage(View iv, int position, MultiPicChooseBean itemData) {
        switch (itemData.type) {
            case 1:
                IntentManager.toBigImageFileActivity((FragmentActivity) getContext(), pathList, position);
                break;
        }
    }


    public void deleteImage(int position, MultiPicChooseBean itemData) {

        pickFileTools.deleteImage(itemData.imgUrlString);
        multiPicChooseBeanList.remove(position);
        pathList.remove(position);
        if (pathList.size() >= 2) {
            MultiPicChooseBean pojo = getBean();
            if (pathList.size() >= selectImageSize) {
                pojo.setVisiable(false);
            } else {
                pojo.setVisiable(true);
            }
        } else {
            Iterator<MultiPicChooseBean> iterator = multiPicChooseBeanList.iterator();
            while (iterator.hasNext()) {
                MultiPicChooseBean next = iterator.next();
                if (next.type == 0)
                    iterator.remove();
            }

            List<MultiPicChooseBean> addImageLists = MultiPicChooseBean.getAddImageBean(pathList.size(), selectImageType);
            multiPicChooseBeanList.addAll(addImageLists);
        }
        adapter.refresh(multiPicChooseBeanList);
    }

    public void setChoosePicButtonView(boolean isShow) {
        MultiPicChooseBean pojo = getBean();
        pojo.setVisiable(isShow);
        adapter.refresh(multiPicChooseBeanList);
    }


    public MultiPicChooseBean getBean() {
        for (MultiPicChooseBean picChoosePojo : multiPicChooseBeanList) {
            if (picChoosePojo.type == 0) {
                return picChoosePojo;
            }
        }
        return null;
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (pickFileTools != null) pickFileTools.onActivityResult(requestCode, resultCode, data);
    }





    public void setSelectImageSize(int selectImageSize) {
        this.selectImageSize = selectImageSize;
    }

    public void setImageList(List<String> imageList) {
        showImageListType = 1;
        pathList.addAll(imageList);
        List<MultiPicChooseBean> multiPicChooseBeans = new ArrayList<>();
        for (int i = 0; i < imageList.size(); i++) {
            String path = imageList.get(i);
            MultiPicChooseBean choosePojo = new MultiPicChooseBean(path, 1, "");
            multiPicChooseBeans.add(choosePojo);
        }
        adapter.refresh(multiPicChooseBeans);
    }

    public void setNestedScrollingEnabled(boolean scrollingEnabled) {
        mBinding.recyclerView.setNestedScrollingEnabled(scrollingEnabled);
    }

    public List<String> getPathList() {
        return pathList;
    }
}
